% CleanBigData.m - MCBS 10/22/2014 
% Modified - MCBS 02/25/2015
% Modified - MCBS 12/05/2020
% This code cleans dataset built by CreatePanelBigData.m and generates
% SugarTransportCost

clear

BaseName = 'G1kmMiR';

load(BaseName)

%% Clean data

% there is an issue with border points for climate variables, because of
% the way climate info was extracted (based on a 5km grid) grid points
% close to the border were zeroed.
% Eliminate those:

GridElimClim = (DS.tmean_growth == 0);
NumElim = sum(GridElimClim);
FracElim = NumElim/length(GridElimClim);
disp(' Eliminate grid points with zeroed climate ');
disp(['   Number of grid points eliminated: ' num2str(NumElim)]);
disp([' Fraction of grid points eliminated: ' num2str(FracElim)]);

GridKeep = ~GridElimClim;

disp(' ')
% soil info not available for lakes and areas in the north  
% need to get map to complement those areas in th north, if want to use
% them

% GridElimSoil = (DS.CEC == 0);
% NumElim = sum(GridElimSoil);
% FracElim = NumElim/length(GridElimSoil);
% disp(' Eliminate grid points with zeroed soil ');
% disp(['   Number of grid points eliminated: ' num2str(NumElim)]);
% disp([' Fraction of grid points eliminated: ' num2str(FracElim)]);

% GridKeep = (GridKeep & ~GridElimSoil);

disp(' ')
% Eliminate Lakes and Urban areas

% GridElimLakeUrban = (DS.Urban == 1 | DS.Lake == 1);
% NumElim = sum(GridElimLakeUrban);
% FracElim = NumElim/length(GridElimLakeUrban);
% disp(' Eliminate grid points in Lakes or Urban:');
% disp(['   Number of grid points eliminated: ' num2str(NumElim)]);
% disp([' Fraction of grid points eliminated: ' num2str(FracElim)]);
% 
% GridKeep = (GridKeep & ~GridElimLakeUrban);
% 
% DS = DS(GridKeep,:);

%% Create SugarTransportCost

% Info from transportation model:
FCR = 32.067; FCUS = 18.24;
VCR = 0.069;  VCUS = 0.039;

DS.SugarTransportCostR  =  FCR + (DS.RoadCostDistance/1000)*VCR; % Sugar transportation cost to closest port in 2010 R$ 

DS.SugarTransportCostUS =  FCUS + (DS.RoadCostDistance/1000)*VCUS;  % Sugar transportation cost to closest port in 2010 US$ 


save([BaseName,'C'],'DS','-v7.3');
